//
//  SearchResultsViewController.swift
//  xdjcy
//
//  Created by Henry on 15/12/7.
//  Copyright (c) 2015年 Henry. All rights reserved.
//

import UIKit

class SearchResultsViewController: UIViewController,UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var search: UISearchBar!
    @IBOutlet weak var tableview: UITableView!
    var searchResults:Array<String>!
    var models:Array<ArticleModel>!
    var url = ""
    var tabName = ""
    override func viewDidLoad() {
        super.viewDidLoad()

        searchResults = Array<String>()
        
        self.tableview.delegate = self
        self.tableview.dataSource = self
        self.search.delegate = self
        search.showsCancelButton = true
        
        search.becomeFirstResponder()
        
        
        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchResults.count
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cellName = "cell"
        var cell = tableView.dequeueReusableCellWithIdentifier(cellName) as? UITableViewCell
        if cell == nil
        {
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellName)
        }
        cell?.textLabel?.text = searchResults[indexPath.row]
        return cell!
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    {
        url = BaseUrl + "cmsArticleController.do?cmsArticleShow2&articleId=\(models[indexPath.row].Id!)"
        tabName = models[indexPath.row].Title!
        var sb = UIStoryboard(name: "Main", bundle: nil)
        var vc = sb.instantiateViewControllerWithIdentifier("webVC") as? WebViewViewController
        vc?.showUrl = url
        vc?.tabName = tabName
        self.navigationController?.pushViewController(vc!, animated: true)
//        self.presentViewController(vc!, animated: true, completion: nil)
    }
    
    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        search.resignFirstResponder()
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        search.resignFirstResponder()
        println("开始搜索")
        println(searchBar.text)
        MenuTypeRN.sharedInstance.SearchList(searchBar.text, did: { (result, message, jsonResult) -> Void in
            
            self.models = Array<ArticleModel>()
            if jsonResult.count > 0
            {
                self.models = jsonResult
                for item in self.models
                {
                    self.searchResults.append(item.Title!)
                }
                
                self.tableview.reloadData()
            }
        })
    }
    
    
    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

        searchResults.removeAll(keepCapacity: false)
        self.tableview.reloadData()
    }
  
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
    }
    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: false)
    }
    
//    func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -> Bool
//    {
//        var index = self.searchDisplayController!.searchBar.selectedScopeButtonIndex
//        var display = self.searchDisplayController!.searchBar.scopeButtonTitles
//        //        self.filterContentForSearchText(searchString, scope: [self.searchDisplayController!.searchBar.scopeButtonTitles][index] as! String)
//        //
//        
//        println(index)
//        println(display)
//        println("开始搜索")
//        return true
//    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
